package com.mj.fragment;

import java.util.ArrayList;

import org.json.JSONObject;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.mj.R;
import com.mj.activity.Activity_Detail;
import com.mj.activity.Activity_Paixu;
import com.mj.activity.Activity_Shaixuan;
import com.mj.adapter.JuyouAdapter;
import com.mj.base.BaseFragment;
import com.mj.bean.Avatar;
import com.mj.http.Json;
import com.mj.http.Url;
import com.mj.util.DensityUtil;

public class Fragment_juyou extends BaseFragment implements AMapLocationListener {
	private LocationManagerProxy locationManager;
	private Double longitude = 0.0, latitude = 0.0;
	private ArrayList<Avatar> avatars = new ArrayList<Avatar>();
	private ViewGroup headerView;
	private ListView listView;
	private JuyouAdapter adapter;

	@Override
	protected int getID() {
		return R.layout.fragment_list;
	}

	@Override
	protected void editView() {
		// title
		aq.id(R.id.tv_title).text("");
		aq.id(R.id.btn_left).background(R.drawable.title_btn_left).text("筛选").clicked(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), Activity_Shaixuan.class);
				startActivity(intent);
			}
		});
		aq.id(R.id.btn_right).background(R.drawable.title_btn_right).text("排序").clicked(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), Activity_Paixu.class);
				startActivity(intent);
			}
		});
		// 定位
		locationManager = LocationManagerProxy.getInstance(getActivity());
		locationManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 5000, 10, this);
		// listview
		headerView = (ViewGroup) inflater.inflate(R.layout.header_juyou, null);
		listView = (ListView) viewGroup.findViewById(R.id.listView1);
		getData();
	}

	public void shaixuan() {

	}

	// 头部
	private void editHeaderView() {
		final Avatar avatar1 = avatars.get(0);
		avatars.remove(avatar1);
		final Avatar avatar2 = avatars.get(0);
		avatars.remove(avatar2);
		final Avatar avatar3 = avatars.get(0);
		avatars.remove(avatar3);
		int margin = DensityUtil.dip2px(getActivity(), 6 * 3);
		int width = (DensityUtil.getDisplay(getActivity()).widthPixels - margin) / 3;
		int bigwidth = DensityUtil.getDisplay(getActivity()).widthPixels - width - margin;
		aq.id(headerView.findViewById(R.id.imageView1)).clicked(new OnClickListener() {
			@Override
			public void onClick(View v) {
				goDetail(avatar1);
			}
		}).width(bigwidth, false).image(avatar1.getAvatar());
		aq.id(headerView.findViewById(R.id.imageView2)).clicked(new OnClickListener() {
			@Override
			public void onClick(View v) {
				goDetail(avatar2);
			}
		}).width(width, false).height(width, false).image(avatar2.getAvatar());
		aq.id(headerView.findViewById(R.id.imageView3)).clicked(new OnClickListener() {
			@Override
			public void onClick(View v) {
				goDetail(avatar3);
			}
		}).width(width, false).height(width, false).image(avatar3.getAvatar());
		aq.id(headerView.findViewById(R.id.tv_name)).text(avatar1.getNickname());
		aq.id(headerView.findViewById(R.id.textView1)).text(avatar1.getDistance() + "米");
		aq.id(headerView.findViewById(R.id.textView2)).text(avatar2.getDistance() + "米");
		aq.id(headerView.findViewById(R.id.textView3)).text(avatar3.getDistance() + "米");
	}

	@Override
	public void onLocationChanged(Location location) {

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

	@Override
	public void onProviderEnabled(String provider) {

	}

	@Override
	public void onProviderDisabled(String provider) {

	}

	@Override
	public void onLocationChanged(AMapLocation location) {
		if (location != null) {
			latitude = location.getLatitude();
			longitude = location.getLongitude();
			locationManager.destroy();
			getData();
		}
	}

	private void getData() {
		baseCallBack callBack = new baseCallBack() {
			@Override
			public void onSuccess(JSONObject object) {
				super.onSuccess(object);
				avatars = Json.getAvatars(object);
				editHeaderView();
				adapter = new JuyouAdapter(getActivity(), avatars);
				listView.addHeaderView(headerView);
				listView.setAdapter(adapter);
				listView.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
						goDetail(avatars.get(position));
					}
				});
			}
		};
		String url = Url.wall + "?latitude=" + latitude + "&longitude=" + longitude;
		baseActivity.getJson(url, callBack, false);
	}

	private void goDetail(Avatar avatar) {
		Intent intent = new Intent(getActivity(), Activity_Detail.class);
		intent.putExtra("avatar", avatar);
		startActivity(intent);
	}
}
